package gui;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import mail.Mail;
import mail.MyModelEmail;
import calendar.CheckboxCalendar;

/**
 * Erzeugt Dialog bei Aufgabeneinladungen
 * 
 * @author Michi
 * 
 */
public class TaskConfirm extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7368577833599390400L;
	private JPanel buttonPanel;
	private GridBagLayout gbl_buttonPanel;
	private String eventID;
	private Connection connection;
	private Statement stat;
	private String[] calDropdown;
	private LinkedList<CheckboxCalendar> cals = new LinkedList<CheckboxCalendar>();
	private JComboBox comboBox;
	private JLabel lblStandarttext;
	private JLabel lblNametext;
	private JLabel lblBistext;
	private JLabel lblPrioritaettext;
	private JLabel lblVontext;
	private JLabel lblNotiztext;
	private Mail activeMail;
	int priority;

	public Mail getActiveMail() {
		return activeMail;
	}

	public void setActiveMail(Mail activeMail) {
		this.activeMail = activeMail;
	}

	/**
	 * Create the panel.
	 */
	public TaskConfirm(Mail mail) {
		setAlwaysOnTop(true);
		setActiveMail(mail);
		setEventID(mail.getText());
		setMinimumSize(new Dimension(450, 350));
		getContentPane().setMinimumSize(new Dimension(0, 200));
		getContentPane().setPreferredSize(new Dimension(0, 200));
		setTitle("Einladung");
		getContentPane().setLayout(new BorderLayout(0, 0));
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblVon = new JLabel("Von:");
		GridBagConstraints gbc_lblVon = new GridBagConstraints();
		gbc_lblVon.anchor = GridBagConstraints.EAST;
		gbc_lblVon.insets = new Insets(0, 0, 5, 5);
		gbc_lblVon.gridx = 1;
		gbc_lblVon.gridy = 0;
		panel.add(lblVon, gbc_lblVon);

		lblVontext = new JLabel("vonText");
		GridBagConstraints gbc_lblVontext = new GridBagConstraints();
		gbc_lblVontext.anchor = GridBagConstraints.WEST;
		gbc_lblVontext.insets = new Insets(0, 0, 5, 0);
		gbc_lblVontext.gridx = 3;
		gbc_lblVontext.gridy = 0;
		panel.add(lblVontext, gbc_lblVontext);

		JLabel dummy = new JLabel("  ");
		GridBagConstraints gbc_dummy = new GridBagConstraints();
		gbc_dummy.insets = new Insets(0, 0, 5, 5);
		gbc_dummy.gridx = 2;
		gbc_dummy.gridy = 1;
		panel.add(dummy, gbc_dummy);

		lblStandarttext = new JLabel("standartText");
		GridBagConstraints gbc_lblStandarttext = new GridBagConstraints();
		gbc_lblStandarttext.anchor = GridBagConstraints.WEST;
		gbc_lblStandarttext.insets = new Insets(0, 0, 5, 0);
		gbc_lblStandarttext.gridx = 3;
		gbc_lblStandarttext.gridy = 2;
		panel.add(lblStandarttext, gbc_lblStandarttext);

		JLabel lblAufgabenname = new JLabel("Aufgabenname:");
		GridBagConstraints gbc_lblAufgabenname = new GridBagConstraints();
		gbc_lblAufgabenname.anchor = GridBagConstraints.EAST;
		gbc_lblAufgabenname.insets = new Insets(0, 0, 5, 5);
		gbc_lblAufgabenname.gridx = 1;
		gbc_lblAufgabenname.gridy = 5;
		panel.add(lblAufgabenname, gbc_lblAufgabenname);

		lblNametext = new JLabel("nameText");
		GridBagConstraints gbc_lblNametext = new GridBagConstraints();
		gbc_lblNametext.anchor = GridBagConstraints.WEST;
		gbc_lblNametext.insets = new Insets(0, 0, 5, 0);
		gbc_lblNametext.gridx = 3;
		gbc_lblNametext.gridy = 5;
		panel.add(lblNametext, gbc_lblNametext);

		JLabel lblBis = new JLabel("Bis:");
		GridBagConstraints gbc_lblBis = new GridBagConstraints();
		gbc_lblBis.anchor = GridBagConstraints.EAST;
		gbc_lblBis.insets = new Insets(0, 0, 5, 5);
		gbc_lblBis.gridx = 1;
		gbc_lblBis.gridy = 6;
		panel.add(lblBis, gbc_lblBis);

		lblBistext = new JLabel("bisText");
		GridBagConstraints gbc_lblBistext = new GridBagConstraints();
		gbc_lblBistext.anchor = GridBagConstraints.WEST;
		gbc_lblBistext.insets = new Insets(0, 0, 5, 0);
		gbc_lblBistext.gridx = 3;
		gbc_lblBistext.gridy = 6;
		panel.add(lblBistext, gbc_lblBistext);

		JLabel lblPriotitt = new JLabel("Priorität:");
		GridBagConstraints gbc_lblPriotitt = new GridBagConstraints();
		gbc_lblPriotitt.anchor = GridBagConstraints.EAST;
		gbc_lblPriotitt.insets = new Insets(0, 0, 5, 5);
		gbc_lblPriotitt.gridx = 1;
		gbc_lblPriotitt.gridy = 7;
		panel.add(lblPriotitt, gbc_lblPriotitt);

		lblPrioritaettext = new JLabel("prioritaetText");
		GridBagConstraints gbc_lblPrioritaettext = new GridBagConstraints();
		gbc_lblPrioritaettext.anchor = GridBagConstraints.WEST;
		gbc_lblPrioritaettext.insets = new Insets(0, 0, 5, 0);
		gbc_lblPrioritaettext.gridx = 3;
		gbc_lblPrioritaettext.gridy = 7;
		panel.add(lblPrioritaettext, gbc_lblPrioritaettext);

		JLabel lblNotiz = new JLabel("Notiz:");
		GridBagConstraints gbc_lblNotiz = new GridBagConstraints();
		gbc_lblNotiz.anchor = GridBagConstraints.EAST;
		gbc_lblNotiz.insets = new Insets(0, 0, 5, 5);
		gbc_lblNotiz.gridx = 1;
		gbc_lblNotiz.gridy = 8;
		panel.add(lblNotiz, gbc_lblNotiz);

		lblNotiztext = new JLabel("notizText");
		GridBagConstraints gbc_lblNotiztext = new GridBagConstraints();
		gbc_lblNotiztext.anchor = GridBagConstraints.WEST;
		gbc_lblNotiztext.insets = new Insets(0, 0, 5, 0);
		gbc_lblNotiztext.gridx = 3;
		gbc_lblNotiztext.gridy = 8;
		panel.add(lblNotiztext, gbc_lblNotiztext);

		JLabel lblInKalender = new JLabel("in Kalender:");
		GridBagConstraints gbc_lblInKalender = new GridBagConstraints();
		gbc_lblInKalender.anchor = GridBagConstraints.EAST;
		gbc_lblInKalender.insets = new Insets(0, 0, 5, 5);
		gbc_lblInKalender.gridx = 1;
		gbc_lblInKalender.gridy = 9;
		panel.add(lblInKalender, gbc_lblInKalender);

		JLabel dummy1 = new JLabel("  ");
		GridBagConstraints gbc_dummy1 = new GridBagConstraints();
		gbc_dummy1.insets = new Insets(0, 0, 5, 0);
		gbc_dummy1.gridx = 3;
		gbc_dummy1.gridy = 10;
		panel.add(dummy1, gbc_dummy1);

		JLabel lblZuKalenderHinzufgen = new JLabel("Zu Kalender hinzufügen?");
		GridBagConstraints gbc_lblZuKalenderHinzufgen = new GridBagConstraints();
		gbc_lblZuKalenderHinzufgen.anchor = GridBagConstraints.WEST;
		gbc_lblZuKalenderHinzufgen.gridx = 3;
		gbc_lblZuKalenderHinzufgen.gridy = 11;
		panel.add(lblZuKalenderHinzufgen, gbc_lblZuKalenderHinzufgen);

		buttonPanel = new JPanel();
		buttonPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		buttonPanel.setPreferredSize(new Dimension(40, 40));
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);

		gbl_buttonPanel = new GridBagLayout();
		gbl_buttonPanel.columnWidths = new int[] { 0, 0, 0, 0 };
		gbl_buttonPanel.rowHeights = new int[] { 0, 0 };
		gbl_buttonPanel.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_buttonPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		buttonPanel.setLayout(gbl_buttonPanel);

		JButton btnJa = new JButton("Ja");
		btnJa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyModelEmail mme = new MyModelEmail();
				mme.deleteMail(getActiveMail());
				Mail yes = new Mail();
				LinkedList<String> ll = new LinkedList<String>();
				ll.add(getActiveMail().getFrom());
				yes.setTo(ll);
				yes.setBetreff("Zusage zu " + getLblNametext().getText());
				yes.setText("Ja ich werde teilnehmen. \nVielen Dank für die Einladung \nViele Liebe Grüße");

				insertTaskIntoDB();

				mme.sendMail(yes, false);

				dispose();
			}
		});
		GridBagConstraints gbc_btnJa = new GridBagConstraints();
		gbc_btnJa.insets = new Insets(0, 0, 0, 5);
		gbc_btnJa.gridx = 0;
		gbc_btnJa.gridy = 0;
		buttonPanel.add(btnJa, gbc_btnJa);

		JButton btnNein = new JButton("Nein");
		btnNein.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyModelEmail mme = new MyModelEmail();
				mme.deleteMail(getActiveMail());
				Mail nope = new Mail();
				LinkedList<String> ll = new LinkedList<String>();
				ll.add(getActiveMail().getFrom());
				nope.setTo(ll);
				nope.setBetreff("Absage zu " + getLblNametext().getText());
				nope.setText("Leider werde ich nicht teilnehmen.\nTrotzdem vielen Dank für die Einladung \nViele Liebe Grüße");

				mme.sendMail(nope, false);

				dispose();
			}
		});
		GridBagConstraints gbc_btnNein = new GridBagConstraints();
		gbc_btnNein.insets = new Insets(0, 0, 0, 5);
		gbc_btnNein.gridx = 1;
		gbc_btnNein.gridy = 0;
		buttonPanel.add(btnNein, gbc_btnNein);

		connection = tools.createConnection.connect();
		System.out.println("EVENT ID" + getEventID());
		try {
			stat = connection.createStatement();
			stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");
			ResultSet rs = stat
					.executeQuery("SELECT Task.* FROM Task WHERE idTask="
							+ getEventID() + " ORDER BY Task.idTask");

			System.out.println("vor der while ist in der while");
			while (rs.next()) {
				System.out.println("while schleife");
				getLblVontext().setText(getActiveMail().getFrom());
				getLblStandarttext().setText(
						"Sie haben eine Einladung zu folgendem Termin:");
				getLblNametext().setText(rs.getString("name"));

				if (rs.getString("priority").contains("0")) {
					getLblPrioritaettext().setText("niedrig");
				}
				if (rs.getString("priority").contains("1")) {
					getLblPrioritaettext().setText("mittel");
				}
				if (rs.getString("priority").contains("2")) {
					getLblPrioritaettext().setText("hoch");
				}

				priority = Integer.parseInt((rs.getString("priority")));

				try {
					Date d = new SimpleDateFormat("yyyy-MM-dd").parse(rs
							.getString("enddate"));
					String date = new SimpleDateFormat("dd.MM.yyyy").format(d);
					getLblBistext().setText(date);

				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				getLblNotiztext().setText(rs.getString("description"));

			}

			ResultSet idsCalendar = stat
					.executeQuery("SELECT idCalendar, name, idCreator FROM Calendar WHERE idUser = "
							+ Test.iduser + ";");

			while (idsCalendar.next()) {
				CheckboxCalendar temp = new CheckboxCalendar(
						idsCalendar.getInt("idCalendar"),
						idsCalendar.getString("name"),
						idsCalendar.getInt("idCreator"));
				cals.add(temp);
			}

			calDropdown = new String[cals.size()];
			for (int i = 0; i < cals.size(); i++) {
				calDropdown[i] = cals.get(i).getName();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		comboBox = new JComboBox(calDropdown);
		comboBox.setPreferredSize(new Dimension(200, 27));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 3;
		gbc_comboBox.gridy = 9;
		panel.add(comboBox, gbc_comboBox);

	}

	public JLabel getLblNotiztext() {
		return lblNotiztext;
	}

	public void setLblNotiztext(JLabel lblNotiztext) {
		this.lblNotiztext = lblNotiztext;
	}

	public String getEventID() {
		return eventID;
	}

	public void setEventID(String eventID) {
		this.eventID = eventID;
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public Statement getStat() {
		return stat;
	}

	public void setStat(Statement stat) {
		this.stat = stat;
	}

	public String[] getCalDropdown() {
		return calDropdown;
	}

	public void setCalDropdown(String[] calDropdown) {
		this.calDropdown = calDropdown;
	}

	public LinkedList<CheckboxCalendar> getCals() {
		return cals;
	}

	public void setCals(LinkedList<CheckboxCalendar> cals) {
		this.cals = cals;
	}

	public JComboBox getComboBox() {
		return comboBox;
	}

	public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}

	public JLabel getLblStandarttext() {
		return lblStandarttext;
	}

	public void setLblStandarttext(JLabel lblStandarttext) {
		this.lblStandarttext = lblStandarttext;
	}

	public JLabel getLblNametext() {
		return lblNametext;
	}

	public void setLblNametext(JLabel lblNametext) {
		this.lblNametext = lblNametext;
	}

	public JLabel getLblBistext() {
		return lblBistext;
	}

	public void setLblBistext(JLabel lblBistext) {
		this.lblBistext = lblBistext;
	}

	public JLabel getLblPrioritaettext() {
		return lblPrioritaettext;
	}

	public void setLblPrioritaettext(JLabel lblPrioritaettext) {
		this.lblPrioritaettext = lblPrioritaettext;
	}

	public JLabel getLblVontext() {
		return lblVontext;
	}

	public void setLblVontext(JLabel lblVontext) {
		this.lblVontext = lblVontext;
	}

	/**
	 * Convenience Funktion um Task anhand der ID einzutragen in die Datenbank
	 */
	public void insertTaskIntoDB() {
		try {
			stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");
			ResultSet party = stat
					.executeQuery("SELECT idTask FROM `sopra-ws12-awesome_fireballs`.`Task`ORDER BY idTask DESC LIMIT 1;");
			int idTask = 1;
			while (party.next()) {
				idTask = party.getInt(1) + 1;
			}

			int selectedCal = comboBox.getSelectedIndex();
			int idSelectedCal = cals.get(selectedCal).getIdCalendar();

			connection.setAutoCommit(false);
			PreparedStatement pstmt = connection
					.prepareStatement("INSERT INTO `sopra-ws12-awesome_fireballs`.Task VALUES(?, ?, ?, ?, ?, ?, ?)");

			pstmt.setInt(1, idTask);
			// Diese 2 mit Kalender ersetzen
			pstmt.setInt(2, idSelectedCal);

			pstmt.setString(3, getLblNametext().getText());
			pstmt.setString(4, getLblBistext().getText());
			pstmt.setInt(5, priority);
			pstmt.setString(6, getLblNotiztext().getText());
			pstmt.setInt(7, 0);
			pstmt.addBatch();

			pstmt.executeBatch();

			connection.setAutoCommit(true);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
